//
//  SW_CaptureView.m
//  cloud-ios-sdk-ocr-1.0.0
//
//  Created by Snail on 2020/8/21.
//  Copyright © 2020 huawei. All rights reserved.
//

#import "SW_CaptureView.h"

@implementation SW_CaptureView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //设置 背景为clear
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    [[UIColor colorWithWhite:0 alpha:0.5] setFill];
    //半透明区域
    UIRectFill(rect);
    CGRect holeiInterSection = CGRectIntersection(self.captureRect, rect);
    [[UIColor clearColor] setFill];
    UIRectFill(holeiInterSection);

    // 获取当前的图形上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置绘制的颜色
    [[UIColor greenColor] setStroke];
    // 设置线条的宽度
    CGContextSetLineWidth(context, 4.0);
    // 设置线条绘制的起始点
    CGContextMoveToPoint(context, CGRectGetMinX(self.captureRect), CGRectGetMinY(self.captureRect) +35);
    // 添加线条路径
    CGContextAddLineToPoint(context, CGRectGetMinX(self.captureRect), CGRectGetMinY(self.captureRect));
    CGContextAddLineToPoint(context, CGRectGetMinX(self.captureRect) +35, CGRectGetMinY(self.captureRect));
    
    // 设置线条绘制的起始点
    CGContextMoveToPoint(context, CGRectGetMaxX(self.captureRect) -35, CGRectGetMinY(self.captureRect));
    // 添加线条路径
    CGContextAddLineToPoint(context, CGRectGetMaxX(self.captureRect), CGRectGetMinY(self.captureRect));
    CGContextAddLineToPoint(context, CGRectGetMaxX(self.captureRect), CGRectGetMinY(self.captureRect) +35);
    
    // 设置线条绘制的起始点
    CGContextMoveToPoint(context, CGRectGetMaxX(self.captureRect), CGRectGetMaxY(self.captureRect) -35);
    // 添加线条路径
    CGContextAddLineToPoint(context, CGRectGetMaxX(self.captureRect), CGRectGetMaxY(self.captureRect));
    CGContextAddLineToPoint(context, CGRectGetMaxX(self.captureRect) -35, CGRectGetMaxY(self.captureRect));
    
    // 设置线条绘制的起始点
    CGContextMoveToPoint(context, CGRectGetMinX(self.captureRect) +35, CGRectGetMaxY(self.captureRect));
    // 添加线条路径
    CGContextAddLineToPoint(context, CGRectGetMinX(self.captureRect), CGRectGetMaxY(self.captureRect));
    CGContextAddLineToPoint(context, CGRectGetMinX(self.captureRect), CGRectGetMaxY(self.captureRect) -35);

    // 执行绘制路径操作
    CGContextStrokePath(context);

}

- (void)setCaptureRect:(CGRect)captureRect {
    _captureRect =captureRect;
    [self setNeedsDisplay];
}

@end
